/**
 * Created by smp on 2017/4/10.
 */
var modelPath=ROOT_PATH+'/observation/observationCardMonthReport/';
/**
 * 关闭layer
 */
function closeLayer() {
    layer.close(index);
}
/**
 * 使用layer 打开一个页面
 * @param title
 * @param content
 */
function openLayer(title,content) {

    index=layer.open({
        type: 2,
        title: title,
        shadeClose: true,
        area: ['1000px', '700px'],
        content: modelPath+content
    });
}
function result(data) {
    if(data==1){
        layer.msg("保存成功");
        closeLayer();
    }else{
        layer.alert("保存失败");
    }
}
var getSelectedUser = function (data) {
    $("#addUserId").val(data[0].id);
    $("#addUserIdTree").textbox("setValue",data[0].realName);
};
function openDetailPage(id) {
    var title='观察月报详情';
    var content='detail.shtml?id='+id;
    openLayer(title,content);
}
$(function () {
    var DEPTIdIFNULL=null;
    //初始化组织结构树
    $('#department_tree').tree({
        url: ROOT_PATH + '/permission/department/getDepartmentTree.shtml?parentId=0',
        lines:true,
        onSelect: function (node) {
            $('#tb1').form('reset');
            $('#tb2').form('reset');
            //重新按部门加载datagrid
            $('#dg1').datagrid('load', {
                deptId: node.id,
                sort: 'addTime',
                order: 'desc'
            });
        },
        onLoadSuccess: function (node, data) {
            var deptId = data[0].id;
            DEPTIdIFNULL=deptId;
            //初始化加载datagrid
            $('#dg1').datagrid({
                method: 'post',
                url: modelPath + 'list.shtml',
                queryParams: {
                    deptId: deptId,
                    sort: 'addTime',
                    order: 'desc'
                },
                toolbar:'#toolbar',
                //idField: 'id',
                fitColumns: true,
                pagination: true,
                rownumbers: true,
                sortOrder: true,
                striped: true,
                singleSelect:true,
                fit: true,
                columns: [[
                    {field: 'id', width: 80, checkbox: true},
                    {field: 'obsName', title: '报告名称', width: 100, formatter:function(value,row,index){
                        return "<a href='javascript:void(0);' style='color: deepskyblue;' onclick='openDetailPage(\""+row.id+"\")'>"+value+"</a>"
                    },  sortable: true},
                    {field: 'obsYear', title: '年度', width: 80,  sortable: true},
                    {field: 'obsMonth', title: '月度', width: 100, sortable: true},
                    {field: 'obsUnitType', title: '单位性质', width: 100, sortable: true},
                    {field: 'addUserName', title: '登记人', width: 100, sortable: false},
                    {field: 'statusName', title: '状态', width: 100, sortable: false},
                    {field: 'addDeptName', title: '登记部门', width: 100, sortable: false},
                    {field: 'addTime', title: '登记时间', width: 100, sortable: true}
                ]]
            });
        }
    });
    $('#btn_clear').on("click", function () {
        $('#tb1').form('reset');
        $('#tb2').form('reset');
    });
    $('#btn_search').on("click", function () {
        var getSelected = $("#department_tree").tree('getSelected');
        var deptId=DEPTIdIFNULL;
        if(getSelected){
            deptId=getSelected.id
        }
        var obsName = $('#obsName').textbox("getValue");
        var obsAllDos = $('#obsAllDos').textbox("getValue");
        var obsYear = $('#obsYear').combobox("getValue");
        var obsMonth = $('#obsMonth').combobox("getValue");
        var addDeptId = $('#addDeptId').combotree("getValue");
        var addUserId = $('#addUserId').val();
        var beginTime = $('#beginTime').datebox("getValue");
        var endTime = $('#endTime').datebox("getValue");
        var statusName = $('#statusName').textbox("getValue");
        $('#dg1').datagrid('load', {deptId:deptId,obsName:obsName,obsAllDos:obsAllDos,obsYear:obsYear,
            obsMonth:obsMonth,addDeptId:addDeptId,addUserId:addUserId,statusName:statusName,beginTime:beginTime,
            endTime:endTime,sort: 'addTime',order: 'desc'});
    });
    $("#obs_report_edit").on("click",function(){
        var checkedRows=$('#dg1').datagrid('getChecked');

        if(checkedRows.length == 0){
            layer.alert("选择需要编辑的行！");
            return
        }else if (checkedRows.length > 1){
            layer.alert("只能选中一行数据！");
            return
        }
        var id=checkedRows[0].id;
        var title='编辑观察月报';
        var content='edit.shtml?id='+id;
        openLayer(title,content);
    });
    //添加按钮事件
    $('#obs_report_add').on('click',function () {

        var title='添加观察月报';
        var content='add.shtml';
        openLayer(title,content);
    });
    //删除按钮事件
    $('#obs_report_delete').on('click',function () {

        var rows = $('#dg1').datagrid('getSelections');

        if(rows.length != 1){
            layer.alert("请选择需要删除的权限！");
            return
        }
        var id=rows[0].id;
        layer.confirm('是否确定删除当前选中数据？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            $.ajax({
                type:'post',
                url : modelPath+'delete.shtml',
                data :{id:id},
                dataType:"json",
                success: function (result) {
                    if (result.success) {
                        $('#dg1').datagrid('clearSelections').datagrid('load');
                        layer.msg('删除成功', {
                            icon: 1,
                            time: 1000
                        });
                    }
                    else {
                        layer.msg('删除失败', {
                            icon: 2,
                            time: 1000
                        });
                    }
                }
            });
        });

    });
    $('#btn_advanced_search').on("click", function () {
        $('#tb2').toggle();
        $('#dg1').datagrid('resize');
    });
    //初始化下拉树
    $('.parentDepartmentId').combotree({
        url: ROOT_PATH+'/permission/department/getDepartmentTree.shtml?parentId=0'
    });
    $('#addUserIdTree').textbox({
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                WindowManager.openLayer({
                    type: 2,
                    title: '添加分析人',
                    area: ['90%', '70%'],
                    content: ROOT_PATH + '/permission/user/simpleList.shtml?isMulti=0',
                    callBack:getSelectedUser
                });
            }
        }],
        editable: false
    });
});